<?php	include_once('inc/header.php');
		include_once('inc/product.inc.php');	
		

				
	if(isset($_SESSION['id'])) // Nothing special to do, just display pannel home
	{
		$sell_yours = true;
		$user = new User($db, $_SESSION['id']); // We instantiate a new user 
		$nbProducts = getNbProducts($db, $user->id);
		
		if(!isset($_POST['action'])) // In this case the user wants to add a new product, we display the product management form 
		{	
			echo '
		
			<!--page-->
			<div id="banner">
				<div class="container intro_wrapper">
					<div class="inner_content">
						<h2 style="text-transform: uppercase;">Product management</h2>
							<h1 class="title" style="text-transform: uppercase;" id="name">Add a new product</h1>
								<h3>
									<div id="desc">Welcome in your <span class="hue">retail outlet</span>! The more <span>informations</span> you fill, the more your <span>product</span> may attract the <span class="hue">shoppers</span>!
								</div></h3>
							</div>
						</div>
					</div>
				<div class="container wrapper">
					<div class="inner_content">';
					
			echo'		<!-- Text -->
				<div id="sellYours">
					<div class="col-md-7">
						
						<h3>Basic Informations </h3>
						<form novalidate="novalidate" onsubmit="return checkCoords();" enctype="multipart/form-data" method="post" action="sell_yours.php" id="register-form" class="register-form">
						<input name="pr_name" id="pr_name" placeholder="Product name" type="text"/>';
						
						displaySelectCategories($db); // Display a drop down menu

				echo'	<textarea id="pr_shortdesc" name="pr_shortdesc" maxlength="200" placeholder="Short product description (max. 200 caracters). This will appear in the product browsing page."></textarea>	
						<h3>Product details</h3>
						<span><input style="max-width:49%;margin-right:1%;" name="pr_price" id="pr_price" placeholder="Selling price (KR)" type="number"/>
						<input style="max-width:49%" name="pr_stock" id="pr_stock" placeholder="Quantity" min=1 type="number"/></span>
						<textarea name="pr_desc" style="min-height: 200px;" maxlength="5000" placeholder="Long, detailed product description. This will appear in the product overview page. (5000 caracters)" id="pr_desc"></textarea>
						<div class="pad15"></div>
					</div>
					
					<div class="col-md-5">
						<h3>Delivery</h3>
						<label>This area is the place where you would like to meet the customer</label>
						<div id="map">
						<iframe
						  width="100%"
						  height="100%"
						  frameborder="0" style="border:0"
						  src="https://www.google.com/maps/embed/v1/place?key=AIzaSyB0qujj1vZpFojRIwf3BefNyIYZq5FBhE8
							&q=Jonkoping">
						</iframe>
						</div>
						<input name="pr_area" id="pr_area" placeholder="Type an area in Jönköping (ex: Centrum)" type="text"/>
						
						<div class="pad5"></div>
						
						<h3>Product picture </h3>
						<div id="preview_div">
						<div id="upload_pic" ><img src="img/small/cam.png" /></div>
						<img id="preview_mainpic" src="" alt=" "  cropwidth="270" cropheight="200"/>
						</div>
						<div class="col-md-4" id="croptext">
						</div>
						<input name="pr_mainpic" id="pr_mainpic" type="file"/>
						<input type="hidden" id="x" name="x" />
						<input type="hidden" id="y" name="y" />
						<input type="hidden" id="w" name="w" />
						<input type="hidden" id="h" name="h" />
						
						<label class="none" id="pic_description">This is the main picture of your product which will be displayed as a thumb in the product browse page and featured in the product overview. </label><br/>
						<input type="hidden" name="action" value="insertproduct" />
						<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
					
					</div>
					
					
					<div class="col-md-1">
						<input name="pr_paypal" type="checkbox" value="paypal"/>
					</div>
					<div class="col-md-11">
						<label>I accept Paypal transaction.The customer will have the choice between paying online or paying you cash when you meet him. <a href="../about/?op=paypal"></br>More information about Paypal transactions.</a></label><br/>
					</div></div>
					<div class="pad5">
						
		<input type="submit" class="btn btn-primary" value="SELL !" style="height:50px; font-size: 2em;"/>
						</form>	
				</div><!-- /.sellYours -->';
		}
		
		else if(isset($_POST['action']) && $_POST['action'] == "insertproduct") // We get the add-product form and enter the informations once a product  has been submitted 
		{
			$actualdate = date("Y-m-d H:i:s");
			$uploadFilename = "noMainPic.jpg";

			// IMAGE UPLOAD --> include ImageManipulator class
			require_once('inc/ImageManipulator.php');
			$x1=$_POST['x'];
			$y1=$_POST['y'];
			$x2=$x1+$_POST['w'];
			$y2=$y1+$_POST['h'];
			
			if(isset($_FILES['pr_mainpic']))
			{
				if ($_FILES['pr_mainpic']['error'] > 0) {
					echo "Error: " . $_FILES['pr_mainpic']['error'] . "<br />";
				} else {
					// array of valid extensions
					$validExtensions = array('.jpg', '.jpeg', '.gif', '.png');
					// get extension of the uploaded file
					$fileExtension = strrchr($_FILES['pr_mainpic']['name'], ".");
					// check if file Extension is on the list of allowed ones
					if (in_array($fileExtension, $validExtensions)) {
						$newNamePrefix = time() . '_';
						$manipulator = new ImageManipulator($_FILES['pr_mainpic']['tmp_name']);
						$manipulator->save('img/uploads/products/large/' . $newNamePrefix . $_FILES['pr_mainpic']['name']);
						// resizing to 270x200
						$newImage = $manipulator->crop($x1, $y1, $x2, $y2);
						$manipulator->resample(270, 200);
						// saving file to uploads folder
						$manipulator->save('img/uploads/products/thumb/' . $newNamePrefix . $_FILES['pr_mainpic']['name']);
						$uploadFilename=$newNamePrefix . $_FILES['pr_mainpic']['name'];
					} 
				}
			}
		
		
			$req = $db->prepare('INSERT INTO jktrade_products(pr_name, pr_price, pr_cat, pr_shortdesc, pr_desc, pr_date, pr_area, pr_mainpic, pr_sellerID, pr_paypal, pr_sellertype) 
													VALUES(:pr_name, :pr_price, :pr_cat, :pr_shortdesc, :pr_desc, :pr_date, :pr_area, :pr_mainpic, :pr_sellerID, :pr_paypal, :pr_sellertype)');
													
			if(isset($_POST['pr_paypal']))
			{
				$paypal = 1;
			}
			else $paypal = 0; 
			
			$req->execute(array(
				'pr_name' => $_POST['pr_name'],
				'pr_price' => $_POST['pr_price'],
				'pr_cat' => $_POST['pr_cat'],
				'pr_shortdesc' => $_POST['pr_shortdesc'],
				'pr_desc' => $_POST['pr_desc'],
				'pr_date' => $actualdate,
				'pr_area' => $_POST['pr_area'],
				'pr_mainpic' => $uploadFilename,
				'pr_sellerID' => $user->id,
				'pr_paypal' => $paypal,
				'pr_sellertype' => $user->acctype // Redundancy between the two table user & product
				
			));
			$productID= $db->lastInsertId();
			echo '
				
			<!--page-->
			<div id="banner">
				<div class="container intro_wrapper">
					<div class="inner_content">
						<h2 style="text-transform: uppercase;">Product management</h2>
							<h1 class="title" style="text-transform: uppercase; margin: 0 0 0 0" id="name">Add pictures to your product\'s gallery</h1>
							</div>
						</div>
					</div>
				<div class="container wrapper">
					<div class="inner_content">';

			echo'	<!-- Text -->
				<div class="twelve columns">
					<legend class="title">Product successfully added<i class="fa fa-check-circle-o"></i></legend>
						<div class="register">
							<h2>Your product is now <span>available</span> in the <span class="hue">product browser</span> for all the Jönkötrade <span>users</span>. You can add pictures of your product to make it more attractive for eventual buyers.</h2> 
							<div class="main">
							<div id="message"></div>
					
						<form id="uploadimage" action="" method="post" enctype="multipart/form-data">
						<div id="empty"><div class="col-md-4"><img src="img/small/cam.png"></div></div>
						
						<div id="previewer"> </div>
						
						
						<div id="selectImage">
						<label>Add a picture.</label><br/>
						<input type="file" name="file" id="pr_addpic" required />
						<input type="hidden" name="pr_id" value="'.$productID.'" />
						
						</div>
						</form>
						</div>
						
									
						</div>
				</div><!-- /.twelve columns -->';
		}
	}
	
	else // User is not connected 
	{
		echo'	<div class="twelve columns" style="text-align: center;">
				<img src="img/big_oops.png" /><br/><br/>
				<legend class="title">It looks like you\'re not connected !</legend>
						<div class="register">
							<h2><a href="#myModal" data-toggle="modal" data-target="#myModal" >Log In</a> or <a href="account.php?op=register">register</a> to discover all the bargains and be able to submit and<span> sell your products </span> 
									
						</div>
				</div><!-- /.twelve columns -->';
	}
	
	echo'
		</div>
		</div> <!-- /.container -->
		<div class="pad25 hidden-md hidden-lg"></div>
		<!--//page-->';

		
include_once('inc/footer.php'); ?>